package models

import (
	"bzppx-codepub-server/app/utils"
	"github.com/snail007/go-activerecord/mysql"
)

// 操作保存日志表
const (
	// 日志等级设置
	Log_Level_Emegergency = iota
	Log_Level_Alaert
	Log_Level_Critical
	Log_Level_Error
	Log_Level_Warning
	Log_Level_Notice
	Log_Level_Info
	Log_Level_Debug
)
const Table_Log_Name = "log"

type Log struct {
}

var LogModel = Log{}

// GetLogByLogId: 根据 log_id 获取日志
func (l *Log) GetLogByLogId(logId string) (log map[string]string, err error) {
	db := G.DB()
	var rs *mysql.ResultSet
	rs, err = db.Query(db.AR().From(Table_Log_Name).Where(map[string]interface{}{
		"log_id": logId,
	}))
	if err != nil {
		return
	}
	log = rs.Row()
	return
}

// Insert: 插入
func (l *Log) Insert(log map[string]interface{}) (id int64, err error) {
	db := G.DB()
	var rs *mysql.ResultSet
	rs, err = db.Exec(db.AR().Insert(Table_Log_Name, log))
	if err != nil {
		return
	}
	id = rs.LastInsertId
	return
}

// GetLogsByKeywordAndLimit: 根据关键字分页获取日志
func (l *Log) GetLogsByKeywordAndLimit(level, message, username string, limit int, number int) (logs []map[string]string, err error) {
	db := G.DB()
	where := make(map[string]interface{})
	if level != "" {
		where["level"] = level
	}
	if message != "" {
		where["message LIKE"] = "%" + message + "%"
	}
	if username != "" {
		where["username LIKE"] = "%" + username + "%"
	}
	var rs *mysql.ResultSet
	rs, err = db.Query(db.AR().From(Table_Log_Name).Where(where).Limit(limit, number).OrderBy("log_id", "DESC"))
	if err != nil {
		return
	}
	logs = rs.Rows()

	return
}

// GetLogsByLimit: 分页获取日志
func (l *Log) GetLogsByLimit(limit int, number int) (logs []map[string]string, err error) {

	db := G.DB()
	var rs *mysql.ResultSet
	rs, err = db.Query(
		db.AR().
			From(Table_Log_Name).
			Limit(limit, number).
			OrderBy("log_id", "DESC"))
	if err != nil {
		return
	}
	logs = rs.Rows()

	return
}

// CountLogs: 获取日志总数
func (l *Log) CountLogs() (count int64, err error) {

	db := G.DB()
	var rs *mysql.ResultSet
	rs, err = db.Query(
		db.AR().
			Select("count(*) as total").
			From(Table_Log_Name))
	if err != nil {
		return
	}
	count = utils.NewConvert().StringToInt64(rs.Value("total"))
	return
}

// CountLogsByKeyword: 根据关键字获取日志总数
func (l *Log) CountLogsByKeyword(level, message, username string) (count int64, err error) {

	db := G.DB()
	where := make(map[string]interface{})
	if level != "" {
		where["level"] = level
	}
	if message != "" {
		where["message LIKE"] = "%" + message + "%"
	}
	if username != "" {
		where["username LIKE"] = "%" + username + "%"
	}
	var rs *mysql.ResultSet
	rs, err = db.Query(db.AR().
		Select("count(*) as total").
		From(Table_Log_Name).
		Where(where))
	if err != nil {
		return
	}
	count = utils.NewConvert().StringToInt64(rs.Value("total"))
	return
}

/*

CREATE TABLE `cp_log` (
  `log_id` int(10) NOT NULL AUTO_INCREMENT COMMENT '日志id',
  `level` tinyint(3) NOT NULL DEFAULT '6' COMMENT '日志级别',
  `controller` char(100) NOT NULL DEFAULT '' COMMENT '控制器',
  `action` char(100) NOT NULL DEFAULT '' COMMENT '动作',
  `get` text NOT NULL COMMENT 'get参数',
  `post` text NOT NULL COMMENT 'post参数',
  `message` varchar(255) NOT NULL DEFAULT '' COMMENT '信息',
  `ip` char(100) NOT NULL DEFAULT '' COMMENT 'ip地址',
  `user_agent` char(200) NOT NULL DEFAULT '' COMMENT '用户代理',
  `referer` char(100) NOT NULL DEFAULT '' COMMENT 'referer',
  `user_id` int(10) NOT NULL DEFAULT '0' COMMENT '帐号id',
  `username` char(100) NOT NULL DEFAULT '' COMMENT '帐号名',
  `create_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
  PRIMARY KEY (`log_id`)
) ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=utf8 COMMENT='行为日志表'

*/
